import { createStore } from 'vuex'
import { Toast } from 'vant'
import user from './modules/user'
import createPersistedstate from 'vuex-persistedstate'
export default createStore({
  state:{
    goodcart:[
      {
        id:1,
        img:'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
        goodsName:'小米11',
        price:5999,
        explain:'xxxxxxx',
        num:1,
        quantity:3,
        checked:false
      },
      {
        id:2,
        img:'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
        goodsName:'小米21',
        price:6999,
        explain:'xxxxxxx',
        num:1,
        quantity:5,
        checked:false
      },
      {
        id:3,
        img:'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg',
        goodsName:'小米13',
        price:8999,
        explain:'xxxxxxx',
        num:1,
        quantity:8,
        checked:false
      }
    ]
  },
  mutations: {
    // 步进器增加
    setGoodsNum(state,id){
    state.goodcart.forEach((item)=>{
      if(item.id===id){
        item.num+=1
      }
    })
    },
    // 步进器change事件
    change(state,id){
      state.goodcart.forEach((item)=>{
        if(item.id===id){
         if(item.num>item.quantity){
            item.num = item.quantity
            Toast.fail('库存不足');
           }
        }
      })
    },
    // 步进器减少事件
    minus(state,id){
      state.goodcart.forEach((item)=>{
        if(item.id===id){
        item.num =  item.num-1
        }
      })
    }
  },
  getters:{

  },
  actions:{

  },
  modules:{
    user
  },
  plugins: [
    createPersistedstate({
      key: 'mi-mall-mobile-store'
    })
  ]
})